﻿using System;
using System.Globalization;
using System.Linq;
using EstateNuke.Data.Properties;

namespace EstateNuke.Data
{
    public partial class EstateNukeCore
    {
        #region Add & Remove DealingReason

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public DealingReason AddDealingReason(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var dealingReason = new DealingReason
                                    {
                                        RowId = Guid.NewGuid(),
                                        CreatedDate = DateTime.Now,
                                    };
            DealingReasons.AddObject(dealingReason);
            dealingReason.IsDeleted = false;
            dealingReason.ModifiedDate = DateTime.Now;
            dealingReason.ModifiedByUser = modifiedUser;
            return dealingReason;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public DealingReason AddDealingReason(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (DealingReasons.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedDealingReason, name));
            }
            var dealingReason = DealingReasons.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (dealingReason == null)
            {
                dealingReason = new DealingReason
                                    {
                                        RowId = Guid.NewGuid(),
                                        CreatedDate = DateTime.Now,
                                    };
                DealingReasons.AddObject(dealingReason);
            }
            dealingReason.IsDeleted = false;
            dealingReason.ModifiedDate = DateTime.Now;
            dealingReason.ModifiedByUser = modifiedUser;
            dealingReason.Name = name;
            return dealingReason;
        }

        /// <summary>
        /// </summary>
        /// <param name = "dealingReason"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveDealingReason(DealingReason dealingReason, User modifiedUser)
        {
            if (dealingReason == null)
            {
                throw new ArgumentNullException("dealingReason");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (DealingReasons.Contains(dealingReason))
            {
                dealingReason.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove InformationSource

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public InformationSource AddInformationSource(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var informationSource = new InformationSource
                                        {
                                            RowId = Guid.NewGuid(),
                                            CreatedDate = DateTime.Now,
                                        };
            InformationSources.AddObject(informationSource);
            informationSource.IsDeleted = false;
            informationSource.ModifiedDate = DateTime.Now;
            informationSource.ModifiedByUser = modifiedUser;
            return informationSource;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public InformationSource AddInformationSource(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InformationSources.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedInformationSource, name));
            }
            var informationSource = InformationSources.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (informationSource == null)
            {
                informationSource = new InformationSource
                                        {
                                            RowId = Guid.NewGuid(),
                                            CreatedDate = DateTime.Now,
                                        };
                InformationSources.AddObject(informationSource);
            }
            informationSource.IsDeleted = false;
            informationSource.ModifiedDate = DateTime.Now;
            informationSource.ModifiedByUser = modifiedUser;
            informationSource.Name = name;
            return informationSource;
        }

        /// <summary>
        /// </summary>
        /// <param name = "informationSource"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveInformationSource(InformationSource informationSource, User modifiedUser)
        {
            if (informationSource == null)
            {
                throw new ArgumentNullException("informationSource");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InformationSources.Contains(informationSource))
            {
                informationSource.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove Facility

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public Facility AddFacility(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var facility = new Facility
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
            Facilities.AddObject(facility);
            facility.IsDeleted = false;
            facility.ModifiedDate = DateTime.Now;
            facility.ModifiedByUser = modifiedUser;
            return facility;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public Facility AddFacility(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (Facilities.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedFacility, name));
            }
            var facility = Facilities.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (facility == null)
            {
                facility = new Facility
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
                Facilities.AddObject(facility);
            }
            facility.IsDeleted = false;
            facility.ModifiedDate = DateTime.Now;
            facility.ModifiedByUser = modifiedUser;
            facility.Name = name;
            return facility;
        }

        /// <summary>
        /// </summary>
        /// <param name = "facility"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveFacility(Facility facility, User modifiedUser)
        {
            if (facility == null)
            {
                throw new ArgumentNullException("facility");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (Facilities.Contains(facility))
            {
                facility.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove Motivation

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public Motivation AddMotivation(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var motivation = new Motivation
                                 {
                                     RowId = Guid.NewGuid(),
                                     CreatedDate = DateTime.Now,
                                 };
            Motivations.AddObject(motivation);
            motivation.IsDeleted = false;
            motivation.ModifiedDate = DateTime.Now;
            motivation.ModifiedByUser = modifiedUser;
            return motivation;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public Motivation AddMotivation(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (Motivations.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedMotivation, name));
            }
            var motivation = Motivations.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (motivation == null)
            {
                motivation = new Motivation
                                 {
                                     RowId = Guid.NewGuid(),
                                     CreatedDate = DateTime.Now,
                                 };
                Motivations.AddObject(motivation);
            }
            motivation.IsDeleted = false;
            motivation.ModifiedDate = DateTime.Now;
            motivation.ModifiedByUser = modifiedUser;
            motivation.Name = name;
            return motivation;
        }

        /// <summary>
        /// </summary>
        /// <param name = "motivation"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveMotivation(Motivation motivation, User modifiedUser)
        {
            if (motivation == null)
            {
                throw new ArgumentNullException("motivation");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (Motivations.Contains(motivation))
            {
                motivation.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove Resistance

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public Resistance AddResistance(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var resistance = new Resistance
                                 {
                                     RowId = Guid.NewGuid(),
                                     CreatedDate = DateTime.Now,
                                 };
            Resistances.AddObject(resistance);
            resistance.IsDeleted = false;
            resistance.ModifiedDate = DateTime.Now;
            resistance.ModifiedByUser = modifiedUser;
            return resistance;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public Resistance AddResistance(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (Resistances.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedResistance, name));
            }
            var resistance = Resistances.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (resistance == null)
            {
                resistance = new Resistance
                                 {
                                     RowId = Guid.NewGuid(),
                                     CreatedDate = DateTime.Now,
                                 };
                Resistances.AddObject(resistance);
            }
            resistance.IsDeleted = false;
            resistance.ModifiedDate = DateTime.Now;
            resistance.ModifiedByUser = modifiedUser;
            resistance.Name = name;
            return resistance;
        }

        /// <summary>
        /// </summary>
        /// <param name = "resistance"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveResistance(Resistance resistance, User modifiedUser)
        {
            if (resistance == null)
            {
                throw new ArgumentNullException("resistance");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (Resistances.Contains(resistance))
            {
                resistance.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove RoomType

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public RoomType AddRoomType(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var roomType = new RoomType
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
            RoomTypes.AddObject(roomType);
            roomType.IsDeleted = false;
            roomType.ModifiedDate = DateTime.Now;
            roomType.ModifiedByUser = modifiedUser;
            roomType.RoomsNumber = 1;
            roomType.HallsNumber = 1;
            roomType.ToiletsNumber = 1;
            roomType.KitchensNumber = 1;
            roomType.BalconiesNumber = 1;
            return roomType;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public RoomType AddRoomType(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (RoomTypes.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedRoomType, name));
            }
            var roomType = RoomTypes.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (roomType == null)
            {
                roomType = new RoomType
                               {
                                   RowId = Guid.NewGuid(),
                                   CreatedDate = DateTime.Now,
                               };
                RoomTypes.AddObject(roomType);
            }
            roomType.IsDeleted = false;
            roomType.ModifiedDate = DateTime.Now;
            roomType.ModifiedByUser = modifiedUser;
            roomType.Name = name;
            roomType.RoomsNumber = 1;
            roomType.HallsNumber = 1;
            roomType.ToiletsNumber = 1;
            roomType.KitchensNumber = 1;
            roomType.BalconiesNumber = 1;
            return roomType;
        }

        /// <summary>
        /// </summary>
        /// <param name = "roomType"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveRoomType(RoomType roomType, User modifiedUser)
        {
            if (roomType == null)
            {
                throw new ArgumentNullException("roomType");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (RoomTypes.Contains(roomType))
            {
                roomType.Delete(modifiedUser);
            }
        }

        #endregion

        #region Add & Remove WithdrawalReason

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public WithdrawalReason AddWithdrawalReason(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var withdrawalReason = new WithdrawalReason
                                       {
                                           RowId = Guid.NewGuid(),
                                           CreatedDate = DateTime.Now,
                                       };
            WithdrawalReasons.AddObject(withdrawalReason);
            withdrawalReason.IsDeleted = false;
            withdrawalReason.ModifiedDate = DateTime.Now;
            withdrawalReason.ModifiedByUser = modifiedUser;
            return withdrawalReason;
        }

        /// <summary>
        /// </summary>
        /// <param name = "name"></param>
        /// <param name = "modifiedUser"></param>
        /// <returns></returns>
        public WithdrawalReason AddWithdrawalReason(string name, User modifiedUser)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (WithdrawalReasons.Any(item => item.IsDeleted == false && item.Name == name))
            {
                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.DuplicatedWithdrawalReason, name));
            }
            var withdrawalReason = WithdrawalReasons.FirstOrDefault(item => item.IsDeleted && item.Name == name);
            if (withdrawalReason == null)
            {
                withdrawalReason = new WithdrawalReason
                                       {
                                           RowId = Guid.NewGuid(),
                                           CreatedDate = DateTime.Now,
                                       };
                WithdrawalReasons.AddObject(withdrawalReason);
            }
            withdrawalReason.IsDeleted = false;
            withdrawalReason.ModifiedDate = DateTime.Now;
            withdrawalReason.ModifiedByUser = modifiedUser;
            withdrawalReason.Name = name;
            return withdrawalReason;
        }

        /// <summary>
        /// </summary>
        /// <param name = "withdrawalReason"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveWithdrawalReason(WithdrawalReason withdrawalReason, User modifiedUser)
        {
            if (withdrawalReason == null)
            {
                throw new ArgumentNullException("withdrawalReason");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (WithdrawalReasons.Contains(withdrawalReason))
            {
                withdrawalReason.Delete(modifiedUser);
            }
        }

        #endregion
    }
}